let allData = {
  indicator: [
    {name: "需求理解", indicatorId: "i1", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {
      name: "数据库设计",
      indicatorId: "i2",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {name: "页面设计", indicatorId: "i3", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {name: "流程设计", indicatorId: "i4", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {name: "架构", indicatorId: "i5", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {name: "编码", indicatorId: "i6", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {
      name: "业务熟悉程度",
      indicatorId: "i7",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {name: "工作态度", indicatorId: "i8", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {name: "目标导向", indicatorId: "i9", description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"},
    {
      name: "团队合作",
      indicatorId: "i10",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {
      name: "数据量化",
      indicatorId: "i11",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {
      name: "时间管理",
      indicatorId: "i12",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {
      name: "风险意识",
      indicatorId: "i13",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {
      name: "学习意愿",
      indicatorId: "i14",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {
      name: "学习效率",
      indicatorId: "i15",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
    {
      name: "分享能力",
      indicatorId: "i16",
      description: "A:balabala B:balabala C:balabala D:balabala E:balabala F:balabala"
    },
  ].reverse(),
  ability: [
    {name: "设计能力", abilityId: "a1", rule: {"i2": 0.4, "i3": 0.3, "i4": 0.3}},
    {name: "编码能力", abilityId: "a2", rule: {"i1": 0.3, "i6": 0.7}},
    {name: "学习能力", abilityId: "a3", rule: {"i14": 0.2, "i16": 0.2, "i15": 0.6}},
    {name: "执行能力", abilityId: "a4", rule: {"i9": 0.3, "i10": 0.3, "i8": 0.4}},
    {name: "项目管理能力", abilityId: "a5", rule: {"i11": 0.25, "i12": 0.25, "i13": 0.25, "i8": 0.25}},
    {name: "业务熟悉", abilityId: "a6", rule: {"i7": 0.75, "i1": 0.25}},
    {name: "领导力", abilityId: "a7", rule: {"i9": 0.45, "i10": 0.25, "i12": 0.3}}
  ].reverse(),

  job: [
    {
      name: "初级研发工程师",
      jobId: "j1",
      rule: {"a1": 0.2, "a2": 0.2, "a3": 0.2, "a4": 0.2, "a6": 0.2},
      important: ["a2", "a4"],
      base: {a1: 40, a3: 20, a6: 20}
    }, {
      name: "中级研发工程师",
      jobId: "j2",
      rule: {"a1": 0.2, "a2": 0.2, "a3": 0.2, "a4": 0.2, "a6": 0.2},
      important: ["a2", "a4"],

      base: {a2: 80, a6: 40}
    },
    {
      name: "高级研发工程师",
      jobId: "j3",
      rule: {"a1": 0.2, "a2": 0.2, "a3": 0.2, "a4": 0.2, "a6": 0.2},
      important: ["a2", "a4"],
      base: {a1: 80, a2: 80, a3: 60, a4: 80}
    }, {
      name: "初级架构师",
      jobId: "j4",
      rule: {"a3": 0.2, "a4": 0.2, "a5": 0.2, "a6": 0.2, "a7": 0.2},
      important: ["a7", "a4", "a6"],
    }, {
      name: "高级架构师",
      jobId: "j5",
      rule: {"a3": 0.2, "a4": 0.2, "a5": 0.2, "a6": 0.2, "a7": 0.2},
      important: ["a7", "a4", "a6"],
      base: {a1: 80, a7: 80}
    },
  ].reverse(),


};

function getId(id) {
  let prefix = id.replace(/^(.*?)\d+$/, "$1");
  let number = new Number(id.replace(/^.*?(\d+)$/, "$1")) + 1;
  return prefix + number;
}

//从缓存中加载数据 模拟数据库
function getData() {
  return allData;
}


function ok(res, data) {
  res.json({msg: "ok", data: data});
}

function listByName(name) {
  let datum = getData()[name];
  return datum == null ? [] : datum;
}

function getById(id, name) {
  let listByName1 = listByName(name);
  for (let i = 0; i < listByName1.length; i++) {
    if (listByName1[i][name + "Id"] == id) {
      return listByName1[i];
    }
  }
  return {};
}

function deleteByName(id, name) {
  let listByName1 = listByName(name);
  for (let i = 0; i < listByName1.length; i++) {
    if (listByName1[i][name + "Id"] == id) {

      listByName1.splice(i, 1);
      break;
    }
  }


}


function saveByName(body, name) {
  let items = listByName(name);

  for (let i = 0; i < items.length; i++) {
    if (items[i].name == body.name) {
      body[name + "Id"] = items[i][name + "Id"];
      items[i] = body;
      break;
    }
  }

  if (body[name + "Id"] == null) {
    body[name + "Id"] = getId(items[0][name + "Id"]);
    items.unshift(body);
  }

  return body;
}


export default {
  "GET /rest/v1/allData": (req, res) => {
    res.json(allData);

  },

  //列出指标
  "GET /rest/v1/indicator/list": listByName("indicator"),

  "GET /rest/v1/indicator/id/*": (req, res) => {
    let id = req.params['0'];
    ok(res, getById(id, "indicator"));
  },
  //保存指标
  "POST /rest/v1/indicator": (req, res) => {
    ok(res, saveByName(req.body, "indicator"));
  },
  //删除指标
  "DELETE /rest/v1/indicator/*": (req, res) => {
    let id = req.params['0'];
    deleteByName(id, "indicator");
    ok(res, "ok");
  },
  //列出能力
  "GET /rest/v1/ability/list": listByName("ability"),
  //保存能力
  "POST /rest/v1/ability": (req, res) => {
    ok(res, saveByName(req.body, "ability"))
  },
  "GET /rest/v1/ability/id/*": (req, res) => {
    let id = req.params['0'];
    ok(res, getById(id, "ability"));
  },
  //删除能力
  "DELETE /rest/v1/ability/*": (req, res) => {
    let id = req.params['0'];
    deleteByName(id, "ability");
    ok(res, "ok");
  },
  //列出职业
  "GET /rest/v1/job/list": listByName("job"),
  //保存职业
  "POST /rest/v1/job": (req, res) => {
    ok(res, saveByName(req.body, "job"))
  },
  "GET /rest/v1/job/id/*": (req, res) => {
    let id = req.params['0'];
    ok(res, getById(id, "job"));
  },
  //删除职业
  "DELETE /rest/v1/job/*": (req, res) => {
    let id = req.params['0'];
    deleteByName(id, "job");
    ok(res, "ok");
  },


}
